package com.yuexun.beilunpatient.base;

import android.content.pm.PackageManager;

import com.umeng.message.PushAgent;
import com.yuexun.beilunpatient.ui.main.bean.UpdateBean;
import com.yuexun.beilunpatient.ui.main.model.impl.UpdateModel;
import com.yuexun.beilunpatient.ui.main.presenter.IUpdatePresenter;
import com.yuexun.beilunpatient.ui.main.presenter.impl.UpdatePresenter;
import com.yuexun.beilunpatient.ui.main.ui.view.IUpdateView;
import com.yuexun.beilunpatient.utils.UpdateManager;

import org.kymjs.kjframe.KJActivity;

/**
 * Created by work-jx on 2016/8/4.
 */
public class BaseKJActivity extends KJActivity implements IUpdateView {

    IUpdatePresenter presenter;
    private UpdateManager updateManager;

    @Override
    public void setRootView() {
        ExitApplication.getInstance().addActivity(this);
        PushAgent.getInstance(aty).onAppStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter = new UpdatePresenter(new UpdateModel(), this);
        presenter.update();
    }

    @Override
    public void showUpdateInfo(UpdateBean updateBean) {
        try {
            int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
            if (updateBean.getVersionCode() > versionCode) {
                if (updateManager == null) {
                    updateManager = new UpdateManager(this, updateBean, versionCode);
                    updateManager.showNoticeDialog();
                } else {
                    if (!updateManager.isShowDialog()) {
                        updateManager.showNoticeDialog();
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void fail() {

    }
}
